#include "XmlRequestMessageHandler.h"

XmlRequestMessageHandler::XmlRequestMessageHandler(void)
{
	attributeCount = 0;
	lsAttributes = new Attribute[MAX_ATTRIBUTE_IN_REQUEST];
	type = Constant::RequestMessage;
}
XmlRequestMessageHandler::XmlRequestMessageHandler(char* subject, char* terminal) {
	this->subject = subject;
	this->terminalType = terminal;
	type = Constant::RequestMessage;
}
XmlRequestMessageHandler::XmlRequestMessageHandler(char* xmlString) : XmlHandler(xmlString) {
	type = Constant::RequestMessage;
	TiXmlElement* terminalNode = rootNode->FirstChildElement("TerminalType");
	terminalType = (char*) terminalNode->GetText();

	TiXmlElement* subjectNode = rootNode->FirstChildElement("Subject");
	subject = (char*) subjectNode->GetText();

	lsAttributes = new Attribute[MAX_ATTRIBUTE_IN_REQUEST];
	attributeCount = 0;
	TiXmlElement* rootAttributesNode = rootNode->FirstChildElement("RequestedAttributes");
	TiXmlNode* attributeNode = 0;
	while(attributeNode = rootAttributesNode->IterateChildren("Attribute", attributeNode)) {
		Attribute* att = new Attribute();
		att->key = (char*) attributeNode->ToElement()->GetText();
		lsAttributes[attributeCount++] = *att;
	}
}
XmlRequestMessageHandler::~XmlRequestMessageHandler(void)
{
}
void XmlRequestMessageHandler::addAttributeType() {
	rootNode->SetAttribute("type", Constant::IssuerMessage);
}

void XmlRequestMessageHandler::addBody() {
	TiXmlElement* terminalNode = new TiXmlElement("TerminalType");	
	terminalNode->InsertEndChild(*(new TiXmlText(terminalType)));
	rootNode->InsertEndChild(*terminalNode);

	TiXmlElement* subjectNode = new TiXmlElement("Subject");	
	subjectNode->InsertEndChild(*(new TiXmlText(subject)));
	rootNode->InsertEndChild(*subjectNode);

	TiXmlElement* attributesNode = new TiXmlElement("RequestedAttributes");	
	for (int i = 0; i < attributeCount; i++) {
		TiXmlElement* attNode = new TiXmlElement("Attribute");	
		attNode->InsertEndChild(*(new TiXmlText(lsAttributes[i].key)));
		attributesNode->InsertEndChild(*attNode);
	}
	rootNode->InsertEndChild(*attributesNode);
}

void XmlRequestMessageHandler::setAttribute(Attribute *attributes, int size) {
	attributeCount = size;
	lsAttributes = attributes;
}